/**
 * 用于在.worker.js文件中导出方法用于在主线程中异步执行
 * @param obj 导出的对象
 */
function expose(obj){
	addEventListener('message',function({data:{type,value}}){
		if(type==='ready'){
			postMessage({
				type:'init',
				value:Object.keys(obj),
			});
		}else if(type==='call'){
			const {method,params,uid}=value;
			try{
				postMessage({
					type:'return',
					value:{
						result:obj[method](...params),
						uid,
					}
				});
			}catch(e){
				postMessage({
					type:'error',
					value:{
						uid,
					}
				});
				throw e;
			}
		}
	});
}
if(!self.requestAnimationFrame){
	self.requestAnimationFrame=function(task){
		setTimeout(task,17);
	};
}

export default expose;
